package au.org.ala.collectors

class Person {

    String familyName
    String foreNames
    String alternativeName
    String periodOfActivity
    int yearOfBirth
    int yearOfDeath
    boolean yearOfBirthUncertain = false
    boolean yearOfDeathUncertain = false
    String areaOfInterest
    String occupation
    String categories                       // space separated list of category
    String biog
    String imageUri
    boolean personContributed
    boolean living
    Date dateCreated
    Date lastUpdated

    static constraints = {
        familyName(blank:false, maxSize:128)
        foreNames(nullable:true, maxSize:256)
        alternativeName(nullable:true, maxSize:256)
        periodOfActivity(nullable:true, maxSize:128)
        areaOfInterest(nullable:true, maxSize:256)
        occupation(nullable:true, maxSize:256)
        categories(nullable:true, validation: {
            def list = it.tokenize(' ')
            list.each { cat ->
                if (!(cat in category)) {
                    return false
                }
            }
            return true
        })
        biog(nullable:true, maxSize:40000)
        imageUri(nullable:true, maxSize:1024)
    }

    static category = ['collector','illustrator','determiner']

    static transients = ['name', 'formattedName', 'period']

    /**
      * Returns concatenated name, eg PAYNE, Christine Lillian
      * @return
      */
     String getName() {
         return familyName + (foreNames ? ", ${foreNames}" : "")
     }

     /**
      * Returns mixed case concatenated name suitable for display, eg Payne, Christine Lillian
      * @return
      */
     String getFormattedName() {
         return Utils.mixedCase(familyName) + (foreNames ? ", ${foreNames}" : "")
     }

     /**
      * Returns the life time or period of activity based on what is known.
      * @return
      */
     String getPeriod() {
         if (yearOfBirth || yearOfDeath) {
             return "(${yearOfBirth?:''} - ${yearOfDeath?:''})"
         } else {
             return periodOfActivity
         }
     }

    List listCategories() {
        if (categories) {
            return categories.tokenize(' ')
        }
        else {
            return []
        }
    }

    boolean hasCategory(String category) {
        if (categories) {
            return (categories =~ category).asBoolean()
        }
        else {
            return false
        }
    }
}
